#include "head_c.h"

int userLoginClient(int net_fd, user_info *info, char *password) {
    //flag=1告诉服务器是登录逻辑
    info->flag = 1;
    sendInfoStruct(net_fd, info);
    recvInfoStruct(net_fd, info);

    if(info->flag == 0) {
        //服务器数据库无此用户
        return -1;
    }
    else {
        //有用户，核对密码
        //printf("password=%s", password);
        char *encrypted = crypt(password, info->command_str_return);
        sprintf(info->password, "%s", encrypted);
        //printf("%s\n", info->password);
        //发送密码哈希给服务器
        sendInfoStruct(net_fd, info);
        //接收登录结果flag
        recvInfoStruct(net_fd, info);
        //printf("flag=%d\n", info->flag);
        if(info->flag == 111) {
            sprintf(info->path, "/%s", info->username);
            return 1;
        }
        else if(info->flag == 222) {
            //密码错误
            return 0;
        }
    }
}
